package com.shan.thread;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * description
 * 中断线程方法
 *
 * @author shanzheng <br/>
 * @date 17:14 2019/1/14.<br/>
 * Modified By <br/>
 */
public class InterruptThreadDemo {
    public static void main(String[] args) {
        InterruptThread interruptThread = new InterruptThread();
        Thread thread = new Thread(interruptThread);
        thread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return;
        }
        thread.interrupt();
        System.out.println("结束" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

class InterruptThread implements Runnable {
    @Override
    public void run() {
        System.out.println("1、进入run()方法 时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            // 线程休眠10秒
            Thread.sleep(10000);
            System.out.println("2、已经完成了休眠,时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        } catch (InterruptedException e) {
            System.out.println("3、休眠被终止,时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            System.out.println(e);
            return;
        }
        System.out.println("4、run()方法正常结束,时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
